/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.ide.IdeView;
import com.intellij.ide.util.PackageUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiPackage;
import com.intellij.util.Icons;

public class CreateComponentAction extends AnAction {
    private Logger logger = Logger.getInstance(getClass().getName());

    public CreateComponentAction() {
        super("Component", "Component", Icons.JUNIT_TEST_CLASS_ICON);
    }

    public void update(AnActionEvent anactionevent) {
        super.update(anactionevent);
        Presentation presentation = anactionevent.getPresentation();
        boolean validAction = checkIfActionIsValid(anactionevent);
        presentation.setEnabled(validAction);
        presentation.setVisible(validAction);
    }

    public void actionPerformed(AnActionEvent anActionEvent) {
        DataContext datacontext = anActionEvent.getDataContext();
        final IdeView ideview = (IdeView) datacontext.getData(DataConstants.IDE_VIEW);
        if (ideview == null) {
            return;
        }
        final Project project = (Project) datacontext.getData(DataConstants.PROJECT);
        final PsiDirectory psidirectory = PackageUtil.getOrChooseDirectory(ideview);
        if (psidirectory == null) {
            return;
        }
        PsiPackage aPackage = psidirectory.getPackage();
        CreateComponentDialog dialog = new CreateComponentDialog(project, aPackage, psidirectory);
        dialog.setModal(false);
        dialog.show();
    }

    private boolean checkIfActionIsValid(AnActionEvent anactionevent) {
        if (anactionevent == null) {
            return false;
        }
        DataContext datacontext = anactionevent.getDataContext();
        IdeView ideview = (IdeView) datacontext.getData("IDEView");
        if (ideview == null) {
            return false;
        }
        PsiDirectory apsidirectory[] = ideview.getDirectories();
        if (apsidirectory.length == 0) {
            return false;
        } else {
            for (int i = 0; i < apsidirectory.length; i++) {
                PsiDirectory psiDirectory = apsidirectory[i];
                if (psiDirectory.getPackage() == null) {
                    return false;
                }
            }
        }
        return true;
    }
}
